angular.module('myapp.AddressSevice', [])

    .factory('AddressSevice', function ($rootScope, $cordovaToast, $timeout, PopupService,$state) {
    //地址
        var MyAddress;

        //获取当前地理坐标
        return{

            getPosition: function (fn) {

                var options = {
                    enableHighAccuracy: true,
                    maximumAge: 3600000
                };

                var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

                function onSuccess(position) {

                    //调用百度地图api将当前获取坐标转换为详细地址
                    var convertor = new BMap.Convertor();
                    var pointArr = [];
                    pointArr.push(new BMap.Point(position.coords.longitude, position.coords.latitude));
                    convertor.translate(pointArr, 1, 5, function(data) {
                        if (data.status === 0) {
                            var geoc = new BMap.Geocoder();
                            var point = new BMap.Point(data.points[0].lng, data.points[0].lat);
                            geoc.getLocation(point, function(rs) {
                                //获取地理位置经纬度
                                // console.log(rs.point);
                                // MyAddress = rs.addressComponents.city + rs.addressComponents.province + '市' + rs.addressComponents.district
                                localStorage.setItem('MyAddress', JSON.stringify(MyAddress));
                                fn(rs.point);
                                // return(rs.point);
                            });
                        }
                    });
                }

                function onError(error) {
                    // alert('code: '    + error.code    + '\n' +
                    // 'message: ' + error.message + '\n');
                }
            }

        // getPosition();
        };
    });
